﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ObviousCode.UI.Aspect;
using ObviousCode.Aspect.PluginLibrary;
using ObviousCode.UI.Aspect.DefaultPlugins.IconLabel;
using ObviousCode.UI.Aspect.TestHarness.Objects;
using ZooKeeperSample.AnimalRegister.Animals;
using ObviousCode.UI.Aspect.PluginLibrary;
using ZooKeeperSample.Themes;
using ZooKeeperSample.AnimalRegister.Cages;
using ObviousCode.UI.Aspect.PluginLibrary.Interfaces;
using ZooKeeperSample.Factories;

namespace ZooKeeperSample.AnimalRegister
{
    public class ObjectRegistry
    {
        AspectDesigner _designer;

        const string CAGE_KEY = "cage";
        const string ELEPHANT_KEY = "elephant";
        const string PANDA_KEY = "panda";
        
        ChartObjectImages _elephantImages;
        ChartObjectImages _pandaImages;
        ChartObjectImages _cageImages;

        ISurfaceObjectFactory _elephantFactory;
        ISurfaceObjectFactory _pandaFactory;
        ISurfaceObjectFactory _cageFactory;

        private ObjectRegistry(AspectDesigner designer)
        {
            _designer = designer;            

            RegisterInternal(designer);
        }

        private void RegisterInternal(AspectDesigner designer)
        {
            designer.BeginRegistration();

            LoadImages();

            RegisterSurfaceObjects(designer);

            AddCategories(designer);

            designer.EndRegistration();

        }

        //Build associated images by linking a key to an image
        private void LoadImages()
        {
            _elephantImages = new ChartObjectImages();
            _pandaImages = new ChartObjectImages();
            _cageImages = new ChartObjectImages();

            _elephantImages.AddTreeImage("elephant", Images.Elephant);
            _elephantImages.Icon = Images.Elephant;

            _pandaImages.AddTreeImage("panda", Images.Panda);
            _pandaImages.Icon = Images.Panda;

            _cageImages.AddTreeImage("cage", Images.House);
            _cageImages.Icon = Images.House;
        }        

        //register items with their 'ui element factories' as generic parameter
        //Key is key to required type in generically specified ui element factory
        //Description is used as a logical string where require on ui
        //Factory builds a new instance of registered type
        //Images is the Image Factory built above
        //Theme describes how the item shall be drawn on the ui, and is used by the generically specified factory.
        private void RegisterSurfaceObjects(AspectDesigner designer)
        {
            _elephantFactory = designer.RegisterObjects<IconLabelFactory> (ELEPHANT_KEY, "Elephant", 
               new ElephantFactory(), _elephantImages, new ZooRoundedBorderTheme());

            _pandaFactory = designer.RegisterObjects<IconLabelFactory> (PANDA_KEY, "Panda", 
                new PandaFactory(), _pandaImages, new ZooRoundedBorderTheme());

            _cageFactory = designer.RegisterObjects<IconLabelFactory>(CAGE_KEY, "Cage", 
                new CageFactory(), _cageImages, new BasicTheme());
        }

        //Only required if you want a more complex hierarchy than registered type -> registered instances
        private void AddCategories(AspectDesigner designer)
        {
            designer.AddCategory("Animals");

            _elephantFactory[ELEPHANT_KEY].Properties.Set("category", "Animals");
            _pandaFactory[PANDA_KEY].Properties.Set("category", "Animals");            
        }

        public static void Register(AspectDesigner designer)
        {
            ObjectRegistry registry = new ObjectRegistry(designer);
        }
    }
}
